const formatSecondsToHMS = (seconds: number) => {
    const h = Math.floor(seconds / 3600)
        .toString()
        .padStart(2, "0");
    const m = Math.floor((seconds % 3600) / 60)
        .toString()
        .padStart(2, "0");
    const s = Math.floor(seconds % 60)
        .toString()
        .padStart(2, "0");
    return `${h}:${m}:${s}`;
};

/**
 * 字符串 → 二进制（每字节 8 位，空格分隔）
 */
const stringToBinary = (str: string): string => {
    return Array.from(new TextEncoder().encode(str))
        .map((b) => b.toString(2).padStart(8, "0"))
        .join(" ");
};

/**
 * 二进制（空格分隔的 8 位）→ 字符串
 */
const binaryToString = (binStr: string): string => {
    const bytes = binStr
        .trim()
        .split(/\s+/)
        .map((b) => Number.parseInt(b, 2));

    if (bytes.some((n) => Number.isNaN(n) || n < 0 || n > 255)) {
        throw new TypeError("Invalid binary string");
    }

    return new TextDecoder().decode(new Uint8Array(bytes));
};

export { formatSecondsToHMS, stringToBinary, binaryToString };
